﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NMA.Web.Controllers
{
    using NMA.Web.Core.Extensions;

    public class BaseController : Controller
    {
        /// <summary>
        /// Method called when an unhandled exception occurs in the action.
        /// http://blog.dantup.me.uk/2009/04/aspnet-mvc-handleerror-attribute-custom.html
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.HttpContext.IsCustomErrorEnabled)
            {
                filterContext.ExceptionHandled = true;
                this.View("Error").ExecuteResult(this.ControllerContext);
            }
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }

        /// <summary>        
        /// Responses the error to client, the JSON object will have proeprty MessageFormatted = false                
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        protected ActionResult ResponseError(string errorMessage)
        {
            ErrorResponsor er = new ErrorResponsor();
            er.WriteErrorResponse(errorMessage);
            return null;
        }

        /// <summary>
        /// Responses the error to client, the JSON object will have proeprty MessageFormatted = true 
        /// </summary>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        protected ActionResult ResponseErrorToClient(string errorMessage)
        {
            ErrorResponsor er = new ErrorResponsor();
            er.WriteErrorResponseToClient(errorMessage);
            return null;
        }

        protected string RenderPartialView(string viewName, object viewData)
        {
            var existingContext = System.Web.HttpContext.Current;
            var writer = new StringWriter();
            var response = new HttpResponse(writer);
            var httpContext = new System.Web.HttpContext(existingContext.Request, response);

            var viewResult = PartialView(viewName, viewData);

            System.Web.HttpContext.Current = httpContext;

            viewResult.ExecuteResult(this.ControllerContext);

            System.Web.HttpContext.Current = existingContext;
            var viewAsHtml = writer.ToString();
            return viewAsHtml;
        }

        /// <summary>
        /// Render partial view, copied from MVC source code
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="viewData">The view data.</param>
        /// <returns></returns>
        private ViewResultBase PartialViewRender(string viewName, object viewData)
        {
            if (viewData != null)
            {
                this.ViewData.Model = viewData;
            }

            return new PartialViewResult
            {
                ViewName = viewName,
                ViewData = this.ViewData,
                TempData = this.TempData
            };
        }
    }
}